package com.android.test.activity;

import com.android.test.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MenuProfileActivity extends Activity {
    private LinearLayout dlgLay = null;
    private boolean bProfileSet = false;
    private ProfilePreference userPrefer = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		userPrefer = new ProfilePreference(this);
		if (userPrefer.getValueByKey(ProfilePreference.COLUMN_USERNAME)
			.length() > 0) {
		    bProfileSet = true;
		}
		showProfileEditDialog(bProfileSet);
    }



    private void showProfileEditDialog(boolean flag) {
		AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
		alertBuilder.setTitle(R.string.title_profile_write_dialog);
		LayoutInflater inflater = LayoutInflater.from(this);
		dlgLay = (LinearLayout) inflater.inflate(R.layout.dialog_profile_edit,
			null);
		if (flag) {
		    readAndSetContentFromProfile();
		}
		alertBuilder.setView(dlgLay);
		alertBuilder.setPositiveButton(R.string.text_confirm,
			edt_confirm_onclicklistener);
		alertBuilder.setNegativeButton(R.string.text_abort,
			edt_abort_onclicklistener);
		alertBuilder.setOnCancelListener(new OnCancelListener() {
	
		    public void onCancel(DialogInterface dialog) {
			finish();
		    }
		});
		alertBuilder.create().show();
    }

    private void readAndSetContentFromProfile() {
		final EditText edt_userName = ((EditText) dlgLay
			.findViewById(R.id.edt_name_user));
		edt_userName.setText(userPrefer
			.getValueByKey(ProfilePreference.COLUMN_USERNAME));
		final EditText edt_userNick = ((EditText) dlgLay
			.findViewById(R.id.edt_nick_name_user));
		edt_userNick.setText(userPrefer
			.getValueByKey(ProfilePreference.COLUMN_NICKNAME));
		final EditText edt_userSchool = ((EditText) dlgLay
			.findViewById(R.id.edt_name_school));
		edt_userSchool.setText(userPrefer
			.getValueByKey(ProfilePreference.COLUMN_NAME_SCHOOLE));
		final EditText edt_userGrade = ((EditText) dlgLay
			.findViewById(R.id.edt_name_grade));
		edt_userGrade.setText(userPrefer
			.getValueByKey(ProfilePreference.COLUMN_NAME_GRADE));
		final CheckBox check = (CheckBox) dlgLay.findViewById(R.id.chk_agree);
		check.setChecked(true);
    }

    OnClickListener edt_confirm_onclicklistener = new OnClickListener() {
	public void onClick(DialogInterface dialog, int arg1) {
	    final String userName = ((EditText) dlgLay
		    .findViewById(R.id.edt_name_user)).getText().toString();
	    final String userNick = ((EditText) dlgLay
		    .findViewById(R.id.edt_nick_name_user)).getText()
		    .toString();
	    final String userSchool = ((EditText) dlgLay
		    .findViewById(R.id.edt_name_school)).getText().toString();
	    final String userGrade = ((EditText) dlgLay
		    .findViewById(R.id.edt_name_grade)).getText().toString();
	    if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userNick)
		    || TextUtils.isEmpty(userSchool)
		    || TextUtils.isEmpty(userGrade)) {
		showWarningDialog(R.string.msg_profile_warning_dialog);
		return;
	    } else {
		final CheckBox check = (CheckBox) dlgLay
			.findViewById(R.id.chk_agree);
		if (!check.isChecked()) {
		    showWarningDialog(R.string.msg_warning_not_agree);
		    return;
		}
		userPrefer.SetValueByKey(ProfilePreference.COLUMN_USERNAME,
			userName);
		userPrefer.SetValueByKey(ProfilePreference.COLUMN_NICKNAME,
			userNick);
		userPrefer.SetValueByKey(ProfilePreference.COLUMN_NAME_SCHOOLE,
			userSchool);
		userPrefer.SetValueByKey(ProfilePreference.COLUMN_NAME_GRADE,
			userGrade);
		bProfileSet = true;
		dialog.dismiss();
		if (!bProfileSet) {
		    Toast.makeText(MenuProfileActivity.this,
			    R.string.msg_success_registed, Toast.LENGTH_SHORT)
			    .show();
		}
		finish();
	    }
	}
    };

    OnClickListener edt_abort_onclicklistener = new OnClickListener() {
	public void onClick(DialogInterface dialog, int arg1) {
	    dialog.dismiss();
	    finish();
	 }
    };

    private void showWarningDialog(final int rid) {
	new AlertDialog.Builder(this)
		.setTitle(R.string.title_tip_dialog)
		.setMessage(rid)
		.setNeutralButton(R.string.text_confirm, new OnClickListener() {
		    public void onClick(DialogInterface dialog, int arg1) {
			dialog.dismiss();
		    }
		}).create().show();
    }

    private void showProfileExistsWarningDialog() {
    	showWarningDialog(R.string.msg_profile_already_exists);
    }

}